﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace asp.netWebForms
{
    public partial class Login : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                labelComment.Text = "";
            }
        }

        protected void btnLogin_Click(object sender, EventArgs e)
        {
            var uid = UserName.Text.Trim();
            var pwd = Password.Text.Trim();

            var sql = string.Format("select * from Users where username='{0}'", uid);
            var data = DbHelper.GetData(sql);
            if (data.Rows.Count == 1)
            {
                var item = data.Rows[0];

                var isActived = (bool)item.ItemArray[3];
                var isDeleted = (bool)item.ItemArray[4];
                var password = item.ItemArray[2].ToString();

                if (isDeleted)
                {
                    labelComment.Text = ("<Script>alert('此用户已被删除！')</Script>");
                }
                else
                {
                    if (isActived)
                    {
                        if (pwd == password)
                        {
                            Response.Redirect("default.aspx");
                        }
                        else
                        {
                            labelComment.Text = ("<Script>alert('用户名或密码错误！')</Script>");
                        }
                    }
                    else
                    {
                        labelComment.Text = ("<Script>alert('此用户已被封号！')</Script>");
                    }
                }
            }
            else
            {
                labelComment.Text = ("<Script>alert('此用户不存在！')</Script>");
            }
        }
    }
}